
//睡眠毫秒数
function sleep(time){
	
	var now = new Date().getTime();
	
	while(new Date().getTime() - now <  time){
		
	}
	
}

/**
 * JQuery 构造StartWith方法
 */
String.prototype.startWith = function(s) {
    if (s == null || s == "" || this.length == 0 || s.length > this.length)
        return false;
    if (this.substr(0, s.length) == s)
        return true;
    else
        return false;
    return true;
};

/**
 * JQuery 构造endWith方法
 */
String.prototype.endWith = function(s) {
    if (s == null || s == "" || this.length == 0 || s.length > this.length)
        return false;
    if (this.substring(this.length - s.length) == s)
        return true;
    else
        return false;
    return true;
};

//ajax同步请求调用
function ajaxRequest(requestUrl , params){
	var result;
	$.ajax({
		url : requestUrl ,
		type :'POST',
		data : params ,
		dataType : "json",
		async : false,
		traditional:true,
		contentType : 'application/x-www-form-urlencoded; charset=UTF-8',
		success:function(data){
			result = data;
		}		
	});
	return result;
}

//ajax异步请求调用
function ajaxRequestAsync(requestUrl , params , fun){
	$.ajax({
		url : requestUrl ,
		type :'POST',
		data : params ,
		dataType : "json",
		contentType : 'application/x-www-form-urlencoded; charset=UTF-8',
		success:function(data){
			fun(data);
		}		
	});
}

//ajax同步请求调用
function ajaxRequestByJSONParams(requestUrl , params , fun){
	var result;
	$.ajax({
		url : requestUrl ,
		type :'POST',
		data : JSON.stringify(params) ,
		dataType : "json",
		async : false,
		traditional:true,
		contentType : 'application/json; charset=UTF-8',
		success:function(data){
			result = data;
			if(fun) fun(data);
		}		
	});
	return result;
}

//ajax异步请求调用
function ajaxRequestAsyncByJSONParams(requestUrl , params , fun){
	$.ajax({
		url : requestUrl ,
		type :'POST',
		data : JSON.stringify(params) ,
		dataType : "json",
		contentType : 'application/json; charset=UTF-8',
		success:function(data){
			fun(data);
		}		
	});
}

//js前端默认采用utf-16方式编码；
//后台数据加密前将数据转成了utf-8格式；
//因此解密后需要进行格式转换，从utf-8转为utf-16。
//字符串转base64编码
function strTobase64(str){
	//前端默认采用的是utf-16编码, 转化前先变成utf-8编码,解决中文乱码问题
	return $.base64.encode(utf16to8(str));
}

//base64转字符串
function base64ToStr(baseStr){
	//前端默认采用的是utf-16编码, 转化前先变成utf-8编码,解决中文乱码问题
	return utf8To16($.base64.decode(baseStr , "utf-8"));
}


/**
 * --------------------------------------------------------------------------------------------------------------------
 * utf8转为utf16
 * @param str
 * @returns {string}
 */
function utf8To16(str) {
    var out, i, len, c;
    var char2, char3;
    out = "";
    len = str.length;
    i = 0;
    while (i < len) {
        c = str.charCodeAt(i++);
        switch (c >> 4) {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
                // 0xxxxxxx
                out += str.charAt(i - 1);
                break;
            case 12:
            case 13:
                // 110x xxxx 10xx xxxx
                char2 = str.charCodeAt(i++);
                out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
                break;
            case 14:
                // 1110 xxxx 10xx xxxx 10xx xxxx
                char2 = str.charCodeAt(i++);
                char3 = str.charCodeAt(i++);
                out += String.fromCharCode(((c & 0x0F) << 12) |
                    ((char2 & 0x3F) << 6) |
                    ((char3 & 0x3F) << 0));
                break;
        }
    }
    return out;
}
 
//utf8转utf16
function utf16to8(str) {
    var out, i, len, c;
    out = "";
    len = str.length;
    for(i = 0; i < len; i++) {
        c = str.charCodeAt(i);
        if ((c >= 0x0001) && (c <= 0x007F)) {
            out += str.charAt(i);
        } else if (c > 0x07FF) {
            out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
            out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
            out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
        } else {
            out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
            out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
        }
    }
    return out;
}
 